﻿Imports System.Globalization
Imports System.IO
Partial Public Class PORTTIME
    Inherits System.Web.UI.Page


    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim lbPage As Label = CType(Master.FindControl("lbPage"), Label)
            lbPage.Text = "PORT TIME REPORT"
            For i As Integer = 0 To 4
                With ddlYear
                    .Items.Insert(i, Date.Now.AddYears(i - 2).Year)
                    .Items(i).Value = Date.Now.AddYears(i - 2).Year
                End With
            Next
            ddlYear.SelectedIndex = 2
            If Not (ddlMonth.Items.FindByValue(Date.Now.Month) Is Nothing) Then
                ddlMonth.SelectedValue = Date.Now.Month
            End If

        End If
    End Sub
    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "M")
        End If
    End Sub


    Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        Dim service As New _ReportingService.Report

        Dim optWindow As String = "left=0,top=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no"
        Dim Orientation As Boolean = True
        Dim ReportName As String = String.Empty
        Dim reportDataSource As String = String.Empty
        Dim paraName As String = String.Empty
        Dim paraValue As String = String.Empty
        Dim reportType As String = String.Empty
        Dim urlDisplay As String = String.Empty
        Dim dsReport As New DataSet
        Dim LCT_TIME As String = "0"
        Dim LCT_OCC As String = "0"
        Dim LCT_OVER As String = "0"
        Dim LCT_ACT As String = "0"
        Dim BCT_TIME As String = "0"
        Dim BCT_OCC As String = "0"
        dsReport.Tables.Add(Get_Value())
        dsReport.AcceptChanges()

        ReportName = "Port_Time.rdlc"
        ' dsReport = New DataSet
        reportDataSource = "PORT_TIME_Value"
        Dim desti As String = ""
        If radioPORT.SelectedValue = "B" Then
            desti = "BULK"
        Else
            If (radioDes.SelectedValue = "A") Then
                desti = "DOMESTIC & FOREIGN"
            ElseIf (radioType.SelectedValue = "I") Then
                desti = "DOMESTIC"
            Else
                desti = "FOREIGN"
            End If
        End If
        paraName = "Destination;Month_YEAR;DateTimeNow"
        paraValue = desti & ";" & CInt(ddlMonth.SelectedValue).ToString("00") & "/" & ddlYear.SelectedValue & ";" & DateTime.Now.ToString()
        Orientation = True
        reportType = radioType.SelectedValue

        urlDisplay = service.CreateReporting(dsReport, reportDataSource, reportType, ReportName, paraName, paraValue, Orientation, "")

        Dim oldFile As String = String.Empty
        oldFile = Server.MapPath(urlDisplay)
        Dim fileInfo As FileInfo = New FileInfo(oldFile)
        Dim reportPath As String = "../WriteReport/"

        Dim newFileName As String = DateTime.Now.ToString("_yyMMdd_HHmmss", New CultureInfo("en-US"))
        newFileName = "Port_Time_Report" & newFileName

        If fileInfo.Exists Then
            fileInfo.MoveTo(fileInfo.DirectoryName & "/" & newFileName & fileInfo.Extension)
            urlDisplay = reportPath & newFileName & fileInfo.Extension
        End If

        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "window.open('" + urlDisplay + "','_blank','" + optWindow + "');", True)
    End Sub

    Private Function Get_Value() As DataTable
        Dim dt As DataTable = create()
        Dim destination As String = ""
        If (radioPORT.SelectedValue = "L") Then
            If (radioDes.SelectedValue <> "A") Then
                destination = radioDes.SelectedValue
            End If
        End If
        Dim dtitem As DataTable = clsGet.Get_PRODUCT_monthly_PORT_TIME(radioPORT.SelectedValue, ddlYear.SelectedValue & CInt(ddlMonth.SelectedValue).ToString("00") & "00", ddlYear.SelectedValue & CInt(ddlMonth.SelectedValue).ToString("00") & "32", destination)
        For Each dr As DataRow In dtitem.Rows
            Dim drN As DataRow = dt.NewRow()
            drN.Item("Booking_no") = dr.Item("Booking_no")
            Try
                drN.Item("Date_Of_Berth") = CDate(dr.Item("Berthing_Date")).ToString("dd/yy/") & dr.Item("Berthing_Date").ToString().Split(" ")(2).Substring(2, 2)
            Catch ex As Exception
                drN.Item("Date_Of_Berth") = ""
            End Try

            drN.Item("Vessel_name") = dr.Item("Vehicle_Text")
            drN.Item("DWT") = dr.Item("Vehicle_DWT")
            drN.Item("Wharf") = dr.Item("Wharf")
            Try
                drN.Item("Arrival") = CDate(dr.Item("Anchor_Date")).ToString("dd/yy/") & dr.Item("Anchor_Date").ToString().Split(" ")(2).Substring(2, 2) & " " & dr.Item("Anchor_Time")
            Catch ex As Exception
                drN.Item("Arrival") = ""
            End Try

            Dim ts As Long = 0
            Try
                ts = DateDiff("n", dr.Item("Berthing_Date") & " " & dr.Item("Berthing_TimeFrom"), dr.Item("Berthing_Date") & " " & dr.Item("Berthing_TimeTo"))
                drN.Item("Berth") = (ts \ 60) & ":" & (ts Mod 60).ToString("00")
            Catch ex As Exception
                drN.Item("Berth") = "0:00"
            End Try


            Try
                ts = DateDiff("n", dr.Item("Last_Date") & " " & dr.Item("Last_TimeFrom"), dr.Item("Last_Date") & " " & dr.Item("Last_Time"))
                drN.Item("DeBerth") = (ts \ 60) & ":" & (ts Mod 60).ToString("00")
            Catch ex As Exception
                drN.Item("DeBerth") = "0:00"
            End Try

            drN.Item("Leaved") = dr.Item("Last_Date") & " " & dr.Item("Last_TimeFrom")
            drN.Item("Date_Of_Deberth") = dr.Item("Last_Date")

            Dim dtPro As DataTable = clsGet.Get_Load_Unload_Product(dr.Item("Booking_no"), "")
            Dim c As Integer = 0
            drN.Item("Product_name1") = ""
            drN.Item("QTY_1") = "0"
            drN.Item("Product_name2") = ""
            drN.Item("QTY_2") = "0"
            drN.Item("Product_name3") = ""
            drN.Item("QTY_3") = "0"
            drN.Item("Product_name4") = ""
            drN.Item("QTY_4") = "0"
            For Each dm As DataRow In dtPro.Rows
                If (c = 0) Then
                    Dim dtxx As DataTable = clsGet.Get_Booking_Product(dr.Item("Booking_no"))
                    If (dtxx.Rows.Count > 0) Then
                        drN.Item("LD") = dtxx.Rows(0).Item("Product_Event")
                    Else
                        drN.Item("LD") = "L"
                    End If

                    Try
                        ts = DateDiff("n", dr.Item("Berthing_Date") & " " & dr.Item("Berthing_TimeFrom"), dm.Item("LU_StartActual_Date").ToString.Substring(0, 2) & " " & GetNameMonth(dm.Item("LU_StartActual_Date").ToString.Substring(3, 2)) & " 20" & dm.Item("LU_StartActual_Date").ToString.Substring(6, 2) & " " & dm.Item("LU_StartActual_Time"))
                        drN.Item("Moored") = (ts \ 60) & ":" & (ts Mod 60).ToString("00")
                    Catch ex As Exception
                        drN.Item("Moored") = "0:00"
                    End Try

                    drN.Item("Start") = dm.Item("LU_StartActual_Date") & " " & dm.Item("LU_StartActual_Time")
                End If
                If (c = dtPro.Rows.Count - 1) Then
                    Try
                        ts = DateDiff("n", dm.Item("LU_StopActual_Date").ToString.Substring(0, 2) & " " & GetNameMonth(dm.Item("LU_StopActual_Date").ToString.Substring(3, 2) & " 20" & dm.Item("LU_StopActual_Date").ToString.Substring(6, 2)) & " " & dm.Item("LU_StopActual_Time"), dr.Item("Last_Date") & " " & dr.Item("Last_TimeFrom"))
                        drN.Item("Stop") = dm.Item("LU_StopActual_Date") & " " & dm.Item("LU_StopActual_Time")
                    Catch ex As Exception
                        drN.Item("Stop") = "0:00"
                    End Try
                    drN.Item("DeMoored") = (ts \ 60) & ":" & (ts Mod 60).ToString("00")
                End If
                Dim Value As String = ""
                If (radioPORT.SelectedValue = "L") Then
                    Value = dm.Item("LU_BLMT")
                Else
                    Value = dm.Item("BU_Truck")
                End If
                Select Case (c)
                    Case 0
                        drN.Item("Product_name1") = dm.Item("Product_name")
                        drN.Item("QTY_1") = Value
                    Case 1
                        drN.Item("Product_name2") = dm.Item("Product_name")
                        drN.Item("QTY_2") = Value
                    Case 2
                        drN.Item("Product_name3") = dm.Item("Product_name")
                        drN.Item("QTY_3") = Value
                    Case 3
                        drN.Item("Product_name4") = dm.Item("Product_name")
                        drN.Item("QTY_4") = Value
                End Select
                c += 1
            Next
            dt.Rows.Add(drN)
            dt.AcceptChanges()
        Next
        Return dt
    End Function

    Private Function create() As DataTable
        Dim dt As New DataTable
        dt.Columns.Add("Booking_no", GetType(String))
        dt.Columns.Add("Date_Of_Berth", GetType(String))
        dt.Columns.Add("Vessel_name", GetType(String))
        dt.Columns.Add("DWT", GetType(String))
        dt.Columns.Add("Wharf", GetType(String))
        dt.Columns.Add("LD", GetType(String))
        dt.Columns.Add("Product_name1", GetType(String))
        dt.Columns.Add("QTY_1", GetType(String))
        dt.Columns.Add("Product_name2", GetType(String))
        dt.Columns.Add("QTY_2", GetType(String))
        dt.Columns.Add("Product_name3", GetType(String))
        dt.Columns.Add("QTY_3", GetType(String))
        dt.Columns.Add("Product_name4", GetType(String))
        dt.Columns.Add("QTY_4", GetType(String))
        dt.Columns.Add("Arrival", GetType(String))
        dt.Columns.Add("Berth", GetType(String))
        dt.Columns.Add("Moored", GetType(String))
        dt.Columns.Add("Start", GetType(String))
        dt.Columns.Add("Stop", GetType(String))
        dt.Columns.Add("DeMoored", GetType(String))
        dt.Columns.Add("DeBerth", GetType(String))
        dt.Columns.Add("Leaved", GetType(String))
        dt.Columns.Add("Date_Of_Deberth", GetType(String))
        dt.AcceptChanges()
        Return dt
    End Function

    Private Sub radioPORT_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radioPORT.SelectedIndexChanged
        If (radioPORT.SelectedValue = "L") Then
            rowType.Visible = True
        Else
            rowType.Visible = False
        End If
    End Sub

    Private Function GetNameMonth(ByVal Month As String) As String
        Try
            Select Case (CInt(Month).ToString("00"))
                Case "01"
                    Return "Jan"
                Case "02"
                    Return "Feb"
                Case "03"
                    Return "Mar"
                Case "04"
                    Return "Apr"
                Case "05"
                    Return "May"
                Case "06"
                    Return "Jun"
                Case "07"
                    Return "Jul"
                Case "08"
                    Return "Aug"
                Case "09"
                    Return "Sep"
                Case "10"
                    Return "Oct"
                Case "11"
                    Return "Nov"
                Case "12"
                    Return "Dec"
                Case Else
                    Return ""
            End Select
        Catch ex As Exception
            Return Month
        End Try

    End Function
End Class